<template>
  <div class="login">
    <div class="login-wrap">
      <div class="switch">
        <router-link to="/login" tag="span">登录</router-link>
        <span>·</span>
        <router-link to="/register" tag="span">注册</router-link>
      </div>
      <validation-observer ref="observer" v-slot="{validate}">
        <div class="input-wrap">
          <validation-provider rules="required|email" name="用户名" v-slot="{errors}">
            <div class="user-wrap">
              <input
                type="text"
                v-model="username"
                class="username"
                placeholder="你的昵称"
                autocomplete="off"
              />
              <i class="iconfont iconName">&#xe634;</i>
              <div class="checkNotify">
                <span>{{errors[0]}}</span>
              </div>
            </div>
          </validation-provider>
          <validation-provider rules="required|min:6" name="密码" v-slot="{errors}">
            <div class="user-wrap">
              <input type="password" class="password" v-model="password" placeholder="设置密码" />
              <i class="iconfont iconWord">&#xe622;</i>
              <div class="checkNotify">
                <span>{{errors[0]}}</span>
              </div>
            </div>
          </validation-provider>
          <validation-provider rules="required|min:4" name="验证码" v-slot="{errors}">
            <div class="user-wrap code">
              <input type="text" class="password" v-model="code" placeholder="验证码" />
              <span class="svg" v-html="svg" @click="_getCode(sid)"></span>
            </div>
            <div class="checkNotify">
              <span>{{errors[0]}}</span>
            </div>
          </validation-provider>
        </div>
        <div class="checkInfo">
          <span>
            <input type="checkbox" name id /> 记住我
          </span>
          <span>登录遇到问题?</span>
        </div>
        <div class="btn-login" @click="validate().then(submitLogin)">登录</div>
        <div class="hr"></div>
        <div class="loginMethod">
          <a
            href="https://github.com/login/oauth/authorize?client_id=f017dcd7f9586b01d18f&redirect_uri=http://localhost:8080/#/login/github"
          >
            <i class="iconfont">&#xe615;</i>
          </a>
        </div>
      </validation-observer>
    </div>
  </div>
</template>

<script>
import { ValidationProvider, ValidationObserver } from "vee-validate";
import { getCaptcha } from "../common/mixin";
import { loginUtil } from "../common/loginUtil";
export default {
  components: {
    ValidationProvider,
    ValidationObserver
  },
  mixins: [getCaptcha],
  data() {
    return {
      username: "",
      password: "",
      svg: "",
      code: "", //验证码
      sid: "",
      gitGitCode: ""
    };
  },
  methods: {
    async submitLogin() {
      // 表单验证
      const isValid = await this.$refs.observer.validate();
      if (!isValid) {
        // 表单验证失败
        return;
      }
      // 发送请求
      const res = await this.$http.post("login/login", {
        uname: this.username,
        upassword: this.password,
        code: this.code,
        sid: this.$store.state.sid
      });
      if (res.errno == 0) {
        // 登录成功 存token 跳主页
        window.localStorage.setItem("token", res.token);
        for (let i = 0; i < res.user.goodArticle.length; i++) {
          const element = res.user.goodArticle[i];
          res.user.goodArticle[i]=element.gtid
        }
        for (let i = 0; i < res.user.follow.length; i++) {
          const element = res.user.follow[i];
          res.user.follow[i]=element.fuuid
        }
        
        
        this.$store.commit("saveUser", res.user);
        /**
         * 将点赞过的文章 记录下来
         */

        this.$router.push("/").catch(res => {});
        this.$message.success(`欢迎回来!${res.user.UserName}`);
      } else if (res.errno == 15) {
        // 验证码错误 重置验证码
        this.$message.warning("验证码错误");
        this._getCode(this.$store.state.sid);
        this.code = "";
      } else {
        // 密码账号错误
        this.$message.warning("用户名或密码错误");
        this.password = "";
      }
    }
  }
};
</script>

<style lang='scss' scoped>
.login {
  height: 100%;
  min-height: 700px;
  background-color: #e1e1e1;
  position: absolute;
  left: 0;
  top: 0;
  right: 0;

  .login-wrap {
    background: white;
    width: 400px;
    margin: 60px auto 0;
    padding: 50px 50px 30px;
    text-align: center;
    box-sizing: border-box;
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
    .switch {
      font-size: 18px;
      span {
        margin: 0 10px;
        display: inline-block;
        cursor: pointer;
        height: 40px;
        line-height: 40px;
      }
    }
    .input-wrap {
      margin-top: 30px;
      input {
        display: block;
        outline: none;
        border: 1px solid #dadada;
        background: #f7f7f7;
        height: 50px;
        width: 100%;
        box-sizing: border-box;
        padding: 5px 0 5px 30px;
        border-radius: 2px;
      }
      input:first-child {
        border-bottom: none;
      }
      .user-wrap {
        position: relative;
      }
      .checkNotify {
        width: 100%;
        height: 25px;
      }
      .code {
        display: flex;
        .svg {
          width: 150px;
          height: 50px;
        }
      }
      .iconfont {
        position: absolute;
        left: 5px;
        top: 15px;
        font-size: 20px;
        color: #969696;
      }
    }
    .checkInfo {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 20px 0;
      font-size: 14px;
    }
    .btn-login {
      width: 100%;
      height: 40px;
      border-radius: 999px;
      background: #3194d0;
      color: white;
      line-height: 40px;
      font-size: 16px;
    }
    .hr {
      margin: 40px auto;
      height: 1px;
      width: 80%;
      background: #cccccc;
      position: relative;
      &::before {
        position: absolute;
        margin-top: -7px;
        margin-left: 72px;
        content: "社交账号登录";
        display: block;
        height: 20px;
        width: 100px;
        background: white;
      }
    }
    .loginMethod {
      i {
        margin-right: 28px;
        font-size: 32px;
        cursor: pointer;
      }
      i:nth-child(1) {
        color: #e05244;
      }
      i:nth-child(3) {
        color: #00bb29;
      }
      i:nth-child(2) {
        color: #498ad5;
      }
    }
  }
}
</style>